package com.example.hans.internetandroid;

import android.os.Message;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class HttpUtil {



    //使用异步消息处理机制
    private OnNeedSendFlag mOnNeedSendFlag;

    public void setOnNeedSendFlag(OnNeedSendFlag mOnNeedSendFlag) {
        this.mOnNeedSendFlag = mOnNeedSendFlag;
    }

    public void sendRequest(final String url) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient okHttpClient = null;
                try {

                    okHttpClient = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url(url)
                            .build();

                    Response response = okHttpClient.newCall(request).execute();
                    String responseData = response.body().string();

                    mOnNeedSendFlag.onNeedSeedFinishMsg(responseData);

                } catch (Exception e) {
                    e.printStackTrace();;
                    mOnNeedSendFlag.onNeedSeedErrorMsg(e.getMessage());
                }
            }
        }).start();

    }

    public interface OnNeedSendFlag {
        void onNeedSeedFinishMsg(String finishMessage);
        void onNeedSeedErrorMsg(String errorMessage);
    }

}
